#pragma once
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>

class util
{
public:
    static std::string read_one_line(std::string &msg, const std::string &SEP)
    {
        size_t pos = msg.find(SEP);
        if(pos == std::string::npos)
            return "";
        std::string ret = msg.substr(0, pos);
        msg.erase(0, pos + SEP.size());
        return ret;
    }

    static void parse_first_line(std::string &line, std::string &method, std::string &url, std::string &version)
    {
        std::stringstream ss(line);
        ss >> method >> url >> version;
    }

    static bool read_file(std::string &path, std::string &file)
    {
        std::ifstream ifs;
        ifs.open(path, std::ios::binary);
        if(ifs.is_open() == false)
        {
            std::cout << "文件打开失败" << std::endl;
            return false;
        }

        ifs.seekg(0, std::ios::end);
        size_t fsize = ifs.tellg();
        ifs.seekg(0, std::ios::beg);
        file.resize(fsize);
        ifs.read(&file[0], fsize);
        ifs.close();

        std::cout << "文件读取成功！" << std::endl;
        return true;
    }
};